﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Dcms.UpdateModule;
using Dcms.Common;
using Dcms.Model;
using Dcms.DAL;
public partial class Manage_Modle_Template_FileList : System.Web.UI.Page
{
    protected string BackPreLevelPathStr = "";

    protected string strPermissionID
    {
        get
        {
            return Request.QueryString["PermissionID"] + "";
        }
    }

    protected string CurrLevelPathStr = "<a href=\"FileList.aspx\">根目录/</a>";
    protected string CurrFilePathStr = "";
    protected StringBuilder CurrLevelPathSB = new StringBuilder();
    protected string WebSitePath = "";
    protected Dcms_FileFolderDAL myFileFolderdal = new Dcms_FileFolderDAL();
    protected string FileUpdateOrderStr = "";
    protected string AddFilePath = "";
    public String ROLESTR = "TEMP";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            MyDataBind();
        }
    }
    protected void MyDataBind()
    {
        int FolderNum = 0, FileNum = 0;
        string FolderPathStr = "", FileOrder = "", SortStr = "";
        WebSitePath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        WebSitePath = WebSitePath.Replace('\\', '/');
        if (DcmsRequest.ValidaQueryStr("Path"))
        {
            FolderPathStr = Request.QueryString["Path"].ToString();
            CurrFilePathStr = FolderPathStr;
            AddFilePath = "'Modle/TempLate/FileEdit.aspx?Type=Crate&Path=" + CurrFilePathStr + "'";
            CurrFilePathStr = "'Modle/TempLate/FileFolderEdit.aspx?Action=Crate&Type=folder&Path=" + CurrFilePathStr + "'";
            FolderPathStr = WebSitePath + FolderPathStr + "/";
        }
        else
        {
            CurrFilePathStr = "'Modle/TempLate/FileFolderEdit.aspx?Action=Crate&Type=folder&Path='";
            AddFilePath = "'Modle/TempLate/FileEdit.aspx?Type=Crate&Path='";
            FolderPathStr = WebSitePath;
        }
        if (DcmsRequest.ValidaQueryStr("Type"))
        {
            string TypeStr = Request.QueryString["Type"].ToString();
            if (DcmsRequest.ValidaQueryStr("Name"))
            {
                DcmsFileManagerProcessor.DeleteFileFolder(FolderPathStr + "/" + Request.QueryString["Name"], TypeStr);
            }
        }
        if (DcmsRequest.ValidaQueryStr("Order"))
        {
            FileOrder = Request.QueryString["Order"].ToString();
        }
        if (DcmsRequest.ValidaQueryStr("Sort"))
        {
            SortStr = Request.QueryString["Sort"].ToString();
        }
        if (FolderPathStr.IndexOf(WebSitePath) == 0)//表示在当前目录下操作有效
        {
            string TempFolderPathStr = FolderPathStr;
            string CommPath = "";
            //生成当前路径
            if (FolderPathStr.Length > WebSitePath.Length)
            {
                TempFolderPathStr = FolderPathStr.Substring(WebSitePath.Length, FolderPathStr.Length - WebSitePath.Length - 1);
                HiddCurFilePath.Value = TempFolderPathStr;
                foreach (string FolderName in TempFolderPathStr.Split('/'))
                {
                    CommPath = CommPath + FolderName;
                    CurrLevelPathSB.AppendFormat("<a href=\"FileList.aspx?Path={1}&PermissionID={2}\">{0}</a>", FolderName + "/", CommPath, strPermissionID);
                    CommPath = CommPath + "/";
                }
            }
            CurrLevelPathStr = CurrLevelPathStr + CurrLevelPathSB.ToString();
            if (FolderPathStr.Length > WebSitePath.Length)//生成排序
            {
                TempFolderPathStr = FolderPathStr.Substring(WebSitePath.Length, FolderPathStr.Length - WebSitePath.Length - 1);
                FileUpdateOrderStr = "?Path=" + TempFolderPathStr + "&Order=modifydate&Sort=" + GetOrderStr(SortStr) + "&PermissionID=" + strPermissionID;
            }
            else
            {
                FileUpdateOrderStr = "?Path=&Order=modifydate&Sort=" + GetOrderStr(SortStr) + "&PermissionID=" + strPermissionID;
            }

            //生成返回上一层
            if (FolderPathStr.Length > WebSitePath.Length)
            {
                string tempPath2 = "";
                int TempIndex = 0;
                if (TempFolderPathStr.LastIndexOf('/') == -1)
                {
                    TempIndex = 0;
                }
                else
                {
                    TempIndex = TempFolderPathStr.LastIndexOf('/');
                }
                tempPath2 = TempFolderPathStr.Substring(0, TempIndex);
                BackPreLevelPathStr = "<tr class=\"datalisttr\" bgcolor=\"#F8F8F8\" onmouseover=\"OverColor(this)\"  onmouseout=\"OutColor(this)\"><td colspan=\"5\"><a href=\"FileList.aspx?Path=" + Server.UrlEncode(tempPath2) + "&PermissionID=" + strPermissionID + "\"><img src=\"../../Images/file/IcoLeft.gif\" border=\"0\" alt=\"返回上级\" align=\"absmiddle\" /> 返回上级</a></td></tr>";
            }
            //帮定文件列表
            literalBackHtml.Text = BackPreLevelPathStr;
            LiteralCurrPath.Text = CurrLevelPathStr;
            try
            {
                RepFileList.DataSource = myFileFolderdal.GetDirectories(FolderPathStr, FileOrder, SortStr, WebSitePath, ref FolderNum, ref FileNum);
                RepFileList.DataBind();
                labFileCount.Text = FileNum.ToString();
                LabFolderCount.Text = FolderNum.ToString();
            }
            catch (Exception ex)
            {
                Jscript.Alert("加载文件列表出错，原因是" + ex.Message);
            }
        }
        else
        {
            //提示无权操作其它目录
        }
    }
    protected string GetOrderStr(string SortStr)
    {
        if (SortStr == "")
        {
            return "desc";
        }
        else
        {
            if (SortStr == "asc")
            {
                return "desc";
            }
            else
            {
                return "asc";
            }
        }
    }
    protected void BtnDelAll_Click(object sender, EventArgs e)
    {
        WebSitePath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        WebSitePath = WebSitePath.Replace('\\', '/');
        string OldUrl = Request.Url.AbsoluteUri;
        string FileNameList = Request.Form["CheckKeyId"];
        string[] FileList = FileNameList.Split(',');
        int SucceedCount = 0, FallCount = 0,Tempint=0;
        for (int i = 0; i < FileList.Length; i++)
        {
            if (HiddCurFilePath.Value != "")
            {
                Tempint=DcmsFileManagerProcessor.DeleteFileFolder(WebSitePath + HiddCurFilePath.Value + "/" + FileList[i]);
                if (Tempint == 0)
                {
                    SucceedCount++;
                }
                else
                {
                    FallCount++;
                }
            }
            else
            {
                Tempint=DcmsFileManagerProcessor.DeleteFileFolder(WebSitePath + FileList[i]);
                if (Tempint == 0)
                {
                    SucceedCount++;
                }
                else
                {
                    FallCount++;
                }
            }
        }
        Jscript.AlertAndRedirect("成功删除" + SucceedCount.ToString() + ",个文件，失败" + FallCount.ToString() + "个", OldUrl);
    }
    protected void BtnBatchCreate_Click(object sender, EventArgs e)
    {
        DcmsTempCreate MyDcmsTemp = new DcmsTempCreate();
        string FilePath = "";
        string TempLateDir = ConfigurationManager.AppSettings["TempLatePath"].ToString();
        TempLateDir = Server.MapPath(TempLateDir);
        TempLateDir = TempLateDir.Replace('\\', '/');
        WebSitePath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        WebSitePath = WebSitePath.Replace('\\', '/');
        string FileNameList = Request.Form["CheckKeyId"];
        string[] FileList = FileNameList.Split(',');
        int SucceedCount = 0, FallCount = 0, Tempint = 0;
        for (int i = 0; i < FileList.Length; i++)
        {
            if (HiddCurFilePath.Value != "")
            {
                FilePath=WebSitePath + HiddCurFilePath.Value;
            }
            else
            {
                FilePath=WebSitePath + HiddCurFilePath.Value;
            }
            
            if (FilePath.IndexOf(TempLateDir) == 0)
            {
                string CreateTempPath=FilePath.Substring(TempLateDir.Length,FilePath.Length-TempLateDir.Length);
                Tempint = MyDcmsTemp.CreateTempLate(FileList[i], FilePath +"/"+FileList[i], CreateTempPath);
                if (Tempint == 0)//表示生成成功
                {
                    SucceedCount++;
                }
                else
                {
                    FallCount++;
                }
            }
        }
    
       FilePath = WebSitePath + HiddCurFilePath.Value;
       if (FilePath.Length > TempLateDir.Length)//表示是生成模块，则要改变数据库
       {
           string MyStr = FilePath.Substring(TempLateDir.Length, FilePath.Length - TempLateDir.Length);
           if (MyStr.IndexOf('/') > 0)
           {
               MyStr = MyStr.Substring(0, MyStr.IndexOf('/'));
           }
           if (FallCount > 0)
           {
               DcmsDataBaseHelper.ExecuteSql("update Dcms_TempLate set Template_UpdateTime='" + DateTime.Now.ToString() + "',Template_State=0 where Template_Name='" + MyStr + "'", null);
           }
           else
           {
               DcmsDataBaseHelper.ExecuteSql("update Dcms_TempLate set Template_UpdateTime='" + DateTime.Now.ToString() + "',Template_State=1 where Template_Name='" + MyStr + "'", null);
           }
       }
        
    }
}
